package com.coderknock.wrapper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p></p>
 *
 * @author 三产
 * @version 1.0
 * @date 2017-08-26
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
public class DefinedHttpServletRequestWrapper extends HttpServletRequestWrapper {
    protected final Logger logger = LogManager.getLogger(getClass());
    private HttpSessionWrapper currentSession;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private String sid = "";

    public DefinedHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public DefinedHttpServletRequestWrapper(String sid, HttpServletRequest request) {
        super(request);
        this.sid = sid;
    }

    public DefinedHttpServletRequestWrapper(String sid, HttpServletRequest request,
                                            HttpServletResponse response) {
        super(request);
        this.request = request;
        this.response = response;
        this.sid = sid;
    }

    @Override
    public HttpSession getSession(boolean create) {
        if (currentSession != null) {
            return currentSession;
        }
        if (!create) {
            return null;
        }
        currentSession = new HttpSessionWrapper(sid, request, response);
        return currentSession;
    }

    @Override
    public HttpSession getSession() {
        return getSession(true);
    }

}
